package com.myway.dto.common;

import com.google.common.base.Joiner;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

/**
 * MD5Util
 * 
 * @author Administrator
 *
 */
public class MD5Util {
    private static char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**
     * 对指定的byte数组数据生成MD5码
     * 
     * @param btInput
     * @return
     */
    public final static String mD5(byte[] btInput) {
        try {
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();

            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }

            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 对指定的字符串进行MD5加密
     * 
     * @param filepath
     * @return
     */
    public final static String getFileMD5String(String filepath) {
        FileInputStream in = null;
        ByteArrayOutputStream out = null;

        try {
            File file = new File(filepath);
            in = new FileInputStream(file);

            out = new ByteArrayOutputStream((int) file.length());

            byte[] cache = new byte[1048576];
            for (int i = in.read(cache); i != -1; i = in.read(cache)) {
                out.write(cache, 0, i);
            }

            String str = mD5(out.toByteArray());

            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Closer.close(out);
            Closer.close(in);
        }

        return null;
    }

    /**
     * shiro的md5加密
     * 
     * @param str
     *            源字符串
     * @param salt
     *            盐
     * @param num
     *            随机数
     * @return
     */
//    public static String md5(String str, String salt, int num) {
//        return new Md5Hash(str, salt, num).toString();
//    }

    /**
     * 将指定的字符串用MD5算法加密
     * 
     * @param inStr
     *            源字符串
     * @return 返回加密后的字符串（默认英文大写）
     */
    public static String string2MD5(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] md5Bytes = md5.digest(inStr.getBytes());

            StringBuffer hexValue = new StringBuffer();
            for (int i = 0; i < md5Bytes.length; i++) {
                int val = ((int) md5Bytes[i]) & 0xff;
                if (val < 16) {
                    hexValue.append("0");
                }
                hexValue.append(Integer.toHexString(val));
            }

            return hexValue.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将字符串数组转换为字符串后进行加密
     * 
     * @param strArray
     * @return
     */
    public static String array2MD5(String[] strArray) {
        String linkStr = Joiner.on("").join(strArray);
        return string2MD5(linkStr);
    }

    /**
     * 对给出的String数组进行MD5校验
     * 
     * @param strArray
     * @param md5Val
     * @return
     */
    public static Boolean macValidate(String[] strArray, String md5Val) {
        String iVal = array2MD5(strArray);
        if (iVal.equals(md5Val)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String token = "5631F7686D2BBDDB3C62A9BC4F32990A";
        String key = "E1BFD8471D4C304D5FE0ED5AF796B006";
        String flag = MD5Util.mD5((token + key).getBytes()).toUpperCase();
        System.out.println(flag);
        System.out.println(string2MD5("lslslslslslsls"));
    }
}
